
#import <Foundation/Foundation.h>


@interface DDRSAWrapper : NSObject

#pragma mark - SecKeyRef
- (BOOL)generateSecKeyPairWithKeySize:(NSUInteger)keySize publicKeyRef:(SecKeyRef *)publicKeyRef privateKeyRef:(SecKeyRef *)privateKeyRef;


/// 公钥SecKeyRef 转成 NSData
/// @param givenKey SecKeyRef
- (NSData *)publicKeyBitsFromSecKey:(SecKeyRef)givenKey;
- (SecKeyRef)publicSecKeyFromKeyBits:(NSData *)givenData;

/// 私钥SecKeyRef 转成 NSData
/// @param givenKey SecKeyRef
- (NSData *)privateKeyBitsFromSecKey:(SecKeyRef)givenKey;
- (SecKeyRef)privateSecKeyFromKeyBits:(NSData *)givenData;


#pragma mark - 公钥加密&私钥解密
/*
 padding ： kSecPaddingNone 或 kSecPaddingPKCS1
 */

/// 公钥加密
/// @param key 公钥 SecKeyRef
/// @param plainData 加密的数据
/// @param padding 加密方式
- (NSData *)encryptWithKey:(SecKeyRef)key plainData:(NSData *)plainData padding:(SecPadding)padding;

/// 私钥解密
/// @param key 私钥 SecKeyRef
/// @param cipherData 解密的数据
/// @param padding 解密方式
- (NSData *)decryptWithKey:(SecKeyRef)key cipherData:(NSData *)cipherData padding:(SecPadding)padding;

#pragma mark - 私钥加密&公钥解码
/// 私钥加密
/// @param key 私钥SecKeyRef
/// @param plainData 待加密的Data
- (NSData *)encryptWithPrivateKey:(SecKeyRef)key plainData:(NSData *)plainData;

/// 公钥解码
/// @param publicKey 公钥SecKeyRef
/// @param cipherData 待解码的Data
- (NSData *)decryptWithPublicKey:(SecKeyRef)publicKey cipherData:(NSData *)cipherData;

#pragma mark - 指数和模数
- (NSData *)getPublicKeyExp:(NSData *)pk;
- (NSData *)getPublicKeyMod:(NSData *)pk ;
/*
 在 iOS9 以上的系统 模数前面要加 00，不然会转换失败
 
 const char fixByte = 0;
 NSMutableData * fixedModule = [NSMutableData dataWithBytes:&fixByte length:1];
 [fixedModule appendData:m];
 
 https://github.com/StCredZero/SCZ-BasicEncodingRules-iOS/issues/6#issuecomment-136601437
 
 */

/// 通过模数和指数生成公钥 SecKeyRef
/// @param modBits 模数
/// @param expBits 指数
- (SecKeyRef)publicKeyDataWithMod:(NSData *)modBits exp:(NSData *)expBits;
@end
